﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ToolsLayer;
using Webcar.BLL;
namespace Webcar.SysManage
{
    public partial class SysUpdate : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;
            //ReadSqlFile();
        }

        //private void ReadSqlFile()
        //{
        //   this.TextBox1.Text =  File.ReadAllText(Server.MapPath("updatesql.txt"),System.Text.Encoding.GetEncoding("GB2312"));
        //}
        protected void Button2_Click(object sender, EventArgs e) {

            //string sql = @"select b.name from sysobjects b where b.id in (Select id from syscolumns where name='dept_code') and b.type='U'";
            //DataSet ds = DB.ExecuteDataset(sql);
            //IDbConnection conn = DB.helper.GetConnection(DB.ConnectString);
            //conn.Open();
            //IDbTransaction trans = conn.BeginTransaction();
            //try
            //{
            //    foreach (DataRow row in ds.Tables[0].Rows)
            //    {
            //        string table = row[0].ToString();
            //        if (table.ToLower() == "dept") continue;
            //        string exec = string.Format("delete from {0} where DEPT_CODE not in (select DEPT_CODE from DEPT)", table);
            //        DB.helper.ExecuteNonQuery(trans, CommandType.Text, exec);
            //    }
            //    trans.Commit();
            //    this.Label1.Text = "数据成功处理。";
            //}
            //catch
            //{
            //    trans.Rollback();
            //}
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            IDbConnection conn = DB.helper.GetConnection(DB.ConnectString);
            conn.Open();
            IDbTransaction trans = conn.BeginTransaction();
            try
            {
                string sql = "delete from InfoList where username='" + CurrentUser.LoginUserID + "'";
                DB.helper.ExecuteNonQuery(trans, CommandType.Text, sql);
                InfoChecker checker = new InfoChecker();
                foreach (InfoCheckType m_type in Enum.GetValues(typeof(InfoCheckType)))
                {
                    string tablename = checker.GetTableName(m_type);
                    string pkid = checker.GetTablePrimaryKey(m_type);
                    string alias = checker.GetTableAlias(m_type);
                    string sql2 = string.Format(@"insert into InfoList(infoid,username,tablename)
                        select {0},'{1}','{2}' from {3}", pkid, CurrentUser.LoginUserID, alias, tablename);
                    DB.helper.ExecuteNonQuery(trans, CommandType.Text, sql2);
                }
                trans.Commit();
                this.Label1.Text = "数据成功处理。";
            }
            catch
            {
                trans.Rollback();
            }
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string sql = this.TextBox1.Text;
            try
            {
                ToolsLayer.DB.ExecuteNonQuery(sql);
                this.Label1.Text = "成功更新";
            }
            catch (Exception ex)
            {
                this.Label1.Text =ex.Message;
            }
        }
    }
}
